/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaces;

import conexion.Operaciones;
import javax.swing.JOptionPane;

/**
 *
 * @author vikko
 */
public class PanelNuevaVenta extends javax.swing.JPanel {
    String []titulos= {"No. Serie","Marca","Modelo", "Descripción", "Precio"};
    ModeloPersonalizado modelo= new ModeloPersonalizado (null, titulos);
    FrameVentaBuscaCliente busca= new FrameVentaBuscaCliente();
    float subtotal= 0;
    float iva=0;
    float total=0;
    int r=0;    //indice para incrementar el subtotal
    /** 
     * Creates new form PanelNuevaVenta
     */
    public PanelNuevaVenta() {
        initComponents();
        modelo.getColumnClass(5);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfield_numeroSerie = new javax.swing.JTextField();
        btn_agregarProducto = new javax.swing.JButton();
        panelMuestraProductos = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla_productosVenta = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btn_confirmar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        lbl_subtotal = new javax.swing.JLabel();
        lbl_iva = new javax.swing.JLabel();
        lbl_total = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        combo_tipoEquipo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 579));

        jLabel1.setText("Ingresar No. de Serie:");

        tfield_numeroSerie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfield_numeroSerieActionPerformed(evt);
            }
        });

        btn_agregarProducto.setText("Agregar");
        btn_agregarProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_agregarProductoActionPerformed(evt);
            }
        });

        panelMuestraProductos.setBorder(javax.swing.BorderFactory.createTitledBorder("Productos agregados a la compra actual"));

        tabla_productosVenta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabla_productosVenta.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(tabla_productosVenta);

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel4.setText("Sub-total:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel5.setText("IVA:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Total:");

        btn_confirmar.setText("Confirmar");
        btn_confirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_confirmarActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");

        lbl_subtotal.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbl_subtotal.setForeground(new java.awt.Color(51, 51, 255));
        lbl_subtotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        lbl_iva.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbl_iva.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        lbl_total.setBackground(new java.awt.Color(204, 255, 255));
        lbl_total.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lbl_total.setForeground(new java.awt.Color(0, 0, 153));
        lbl_total.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout panelMuestraProductosLayout = new javax.swing.GroupLayout(panelMuestraProductos);
        panelMuestraProductos.setLayout(panelMuestraProductosLayout);
        panelMuestraProductosLayout.setHorizontalGroup(
            panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMuestraProductosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelMuestraProductosLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_total, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lbl_iva, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lbl_subtotal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(26, 26, 26)
                        .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                            .addComponent(btn_confirmar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        panelMuestraProductosLayout.setVerticalGroup(
            panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelMuestraProductosLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelMuestraProductosLayout.createSequentialGroup()
                        .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btn_confirmar, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_subtotal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelMuestraProductosLayout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addComponent(jButton2))
                            .addGroup(panelMuestraProductosLayout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addGroup(panelMuestraProductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(panelMuestraProductosLayout.createSequentialGroup()
                                        .addComponent(lbl_iva, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(lbl_total, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(panelMuestraProductosLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel6)))
                .addGap(31, 31, 31))
        );

        lbl_total.getAccessibleContext().setAccessibleName("");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/pc_banner2.png"))); // NOI18N

        combo_tipoEquipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PC", "Monitor", "Impresora", "Laptop", "Tablet", "Accesorio" }));

        jLabel3.setText("Selecciona el tipo de equipo:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelMuestraProductos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(combo_tipoEquipo, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(113, 113, 113)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfield_numeroSerie, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn_agregarProducto)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfield_numeroSerie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_agregarProducto)
                    .addComponent(jLabel1)
                    .addComponent(combo_tipoEquipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(panelMuestraProductos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tfield_numeroSerieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfield_numeroSerieActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfield_numeroSerieActionPerformed

    private void btn_agregarProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_agregarProductoActionPerformed
        String n_serie= tfield_numeroSerie.getText();  
        String tipo= combo_tipoEquipo.getSelectedItem().toString();
        Operaciones oper= new Operaciones();
        String []registro= oper.consultaProductoAlmacen(tipo, n_serie);                        
        if (registro[3]!=null){     //Valida que existe una descripcion de producto, si no es así el producto no existe
            modelo.addRow(registro);   
            tabla_productosVenta.setModel(modelo);            
            subtotal+=Float.parseFloat(modelo.getValueAt(r, 4).toString());            
            r++;            
            lbl_subtotal.setText("$ "+String.valueOf(subtotal));
            iva= subtotal * (float)0.16;
            lbl_iva.setText("$ "+iva);
            total= subtotal + iva;
            lbl_total.setText("$ "+total);
        }
        else 
            JOptionPane.showMessageDialog(jScrollPane1, "El producto no existe \nVerifique el No. de serie o el tipo de producto", "Error al agregar producto" , JOptionPane.ERROR_MESSAGE);
        tfield_numeroSerie.setText("");
    }//GEN-LAST:event_btn_agregarProductoActionPerformed

    private void btn_confirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_confirmarActionPerformed
        Object []opciones= {"Nuevo Cliente","Cliente existente"};
        int rows=modelo.getRowCount();
        System.out.println("Numero de renglones: "+rows);
        String detalle_venta="";
        String c1="";
        String c2="";
        String c3="";
        String c4="";
        String c5="";
        for(int i=0;i<rows;i++)  {          
            c1= (String) modelo.getValueAt(i, 0);
            c2= (String) modelo.getValueAt(i, 1);
            c3= (String) modelo.getValueAt(i, 2);
            c4= (String) modelo.getValueAt(i, 3);
            c5= (String) modelo.getValueAt(i, 4);
            detalle_venta+="Producto "+(i+1)+ ":--No.Serie: "+c1+"--Marca: "+c2+"--Modelo: "+c3+"--Descripcion: "+c4+"--Precio: "+c5+"\n";
        }     
                
        System.out.println("Nombre venta: "+this.getParent().getName());
        int sel= JOptionPane.showOptionDialog(this, "Asignar la venta a un:", "Ingresar datos del cliente", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, opciones, opciones[0]);
        if(sel==1){  //Selecciono la opcion Buscar Cliente
            busca.setLocationRelativeTo(null);
            busca.setVisible(true);            
        }
        System.out.println("Detalle: "+detalle_venta);
    }//GEN-LAST:event_btn_confirmarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_agregarProducto;
    private javax.swing.JButton btn_confirmar;
    private javax.swing.JComboBox combo_tipoEquipo;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl_iva;
    private javax.swing.JLabel lbl_subtotal;
    private javax.swing.JLabel lbl_total;
    private javax.swing.JPanel panelMuestraProductos;
    private javax.swing.JTable tabla_productosVenta;
    private javax.swing.JTextField tfield_numeroSerie;
    // End of variables declaration//GEN-END:variables
}
